local extension = Package("skyinyang")
extension.extensionName = "jilvRem"

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["skyinyang"] = "极略三国-阴阳",
}

local zuoci = General(extension, "sk__zuoci", "qun", 3)
local sk__qianhuan = fk.CreateTriggerSkill{
  name = "sk__qianhuan",
  anim_type = "special",
  frequency = Skill.Compulsory,
  events = {fk.GameStart, fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.GameStart then
        return true
      elseif event == fk.TurnStart then
        return target == player
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = player:getMark("sk__qianhuan_deputy") == 0 and 2 or 4
    if player.deputyGeneral ~= "" and player.deputyGeneral ~= "sk__zuoci" then
      room:setPlayerMark(player, "sk__qianhuan_deputy", 1)
      n = 4
      room:returnToGeneralPile({player.deputyGeneral})
      room:changeHero(player, "", false, true, true, false, false)
    end
    if player.dead then return end

    local mark = player:getTableMark("@[SkSkills]sk__qianhuan")
    mark = table.filter(mark, function (s)
      return player:hasSkill(s, true)
    end)
    if #mark > 0 then
      local lose = table.simpleClone(mark)
      local result = room:askForCustomDialog(player, self.name,
        "packages/utility/qml/ChooseSkillBox.qml", {
        mark, 0, 2, "#sk__qianhuan-reserve"
      })
      if result ~= "" and #json.decode(result) > 0 then
        mark = table.simpleClone(json.decode(result))
        for _, s in ipairs(mark) do
          table.removeOne(lose, s)
        end
        room:setPlayerMark(player, self.name, mark)
      end
      if #lose > 0 then
        room:handleAddLoseSkills(player, "-"..table.concat(lose, "|-"), nil, true, false)
      end
    end

    local generals = room:getNGenerals(3)
    if #generals == 0 then return end
    local skills = {}
    for i = 1, #generals, 1 do
      table.insert(skills, Fk.generals[generals[i]]:getSkillNameList(true))
    end
    local result = room:askForCustomDialog(player, self.name,
      "packages/jilvRem/qml/ChooseGeneralSkillsBox.qml", {
      generals, skills, 0, n, "#sk__qianhuan-choice:::"..n, true
    })
    room:returnToGeneralPile(generals)
    local choices = {}
    if result ~= "" then
      choices = json.decode(result)
    end
    if #choices > 0 then
      table.insertTableIfNeed(mark, choices)
      room:setPlayerMark(player, "@[SkSkills]sk__qianhuan", mark)
      room:handleAddLoseSkills(player, table.concat(choices, "|"), nil, true, false)
    end
  end,

  refresh_events = {fk.EventLoseSkill},
  can_refresh = function (self, event, target, player, data)
    return target == player and data == self
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@[SkSkills]sk__qianhuan", 0)
  end,
}
zuoci:addSkill(sk__qianhuan)
Fk:loadTranslationTable{
  ["sk__zuoci"] = "左慈",
  ["#sk__zuoci"] = "",

  ["sk__qianhuan"] = "千幻",
  [":sk__qianhuan"] = "锁定技，游戏开始时，你随机展示三张武将牌并选择获得其中至多两个技能。回合开始时，你可以先保留获得的至多两个技能，"..
  "再选择获得两个技能。（若你有副将，改为移除副将并获得至多四个技能）",
  ["@[SkSkills]sk__qianhuan"] = "千幻",
  ["#sk__qianhuan-reserve"] = "千幻：选择保留的至多两个技能",
  ["#sk__qianhuan-choice"] = "千幻：选择获得至多%arg个技能",

  ["$sk__qianhuan1"] = "万物苍生，幻化由心。",
  ["$sk__qianhuan2"] = "吐故纳新，师法天地。",
  ["~sk__zuoci"] = "腾云跨风，飞升太虚。",
}

local zoushi = General(extension, "sk__zoushi", "qun", 3, 3, General.Female)
local sk__jiaomei = fk.CreateTriggerSkill{
  name = "sk__jiaomei",
  anim_type = "control",
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and (data.card.trueName == "slash" or data.card.type == Card.TypeTrick) and
      player.phase == Player.Play and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and
      table.find(AimGroup:getAllTargets(data.tos), function (id)
        return not player.room:getPlayerById(id).dead
      end)
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#sk__jiaomei-invoke")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, AimGroup:getAllTargets(data.tos))
    for _, id in ipairs(AimGroup:getAllTargets(data.tos)) do
      local p = room:getPlayerById(id)
      if not p.dead then
        if not p.chained then
          p:setChainState(true)
        else
          p:setChainState(false)
          if not p.dead then
            p:turnOver()
          end
        end
      end
    end
  end,
}
local sk__huoshui = fk.CreateTriggerSkill{
  name = "sk__huoshui",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Finish and
      table.find(player.room:getOtherPlayers(player), function(p)
        return p.chained and not p:isNude() or not p.faceup
      end)
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#sk__huoshui-invoke")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(table.filter(room:getOtherPlayers(player), function(p)
      return p.chained and not p:isNude() or not p.faceup
    end), Util.IdMapper)
    room:doIndicate(player.id, targets)
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if p.chained and not p:isNude() and not p.dead and not player.dead then
        local card = room:askForCardChosen(player, p, "he", self.name, "#sk__huoshui-choose::"..p.id)
        room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, self.name, nil, false, player.id)
      end
    end
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if not p.faceup then
        room:damage{
          from = player,
          to = p,
          damage = 1,
          skillName = self.name,
        }
      end
    end
  end,
}
zoushi:addSkill(sk__jiaomei)
zoushi:addSkill(sk__huoshui)
Fk:loadTranslationTable{
  ["sk__zoushi"] = "邹氏",
  ["#sk__zoushi"] = "",

  ["sk__jiaomei"] = "娇媚",
  [":sk__jiaomei"] = "你的出牌阶段内限一次，当你使用【杀】或普通锦囊牌指定目标后，你可以令所有目标角色依次执行：若其武将牌未横置，"..
  "你令其横置；已横置，令其重置并翻面。",
  ["sk__huoshui"] = "祸水",
  [":sk__huoshui"] = "结束阶段，你可以获得所有武将牌横置的其他角色各一张牌，然后对武将牌背面朝上的其他角色各造成1点伤害。",
  ["#sk__jiaomei-invoke"] = "娇媚：是否令未横置的目标横置、已横置的目标重置并翻面？",
  ["#sk__huoshui-invoke"] = "祸水：是否获得横置的角色各一张牌、对翻面的角色各造成1点伤害？",
  ["#sk__huoshui-choose"] = "祸水：获得 %dest 的一张牌",

  ["$sk__jiaomei"] = "数到三，你再转过来吧~",
  ["$sk__huoshui"] = "这点伤，你可经受得？",
  ["~sk__zoushi"] = "",
}

local zhugeguo = General(extension, "sk__zhugeguo", "shu", 3, 3, General.Female)
local sk__yuhua = fk.CreateTriggerSkill{
  name = "sk__yuhua",
  anim_type = "defensive",
  events = {fk.DamageCaused, fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card and data.card.type == Card.TypeTrick
  end,
  on_cost = function(self, event, target, player, data)
    local prompt = "#sk__yuhua1-invoke::"..data.to.id
    if event == fk.DamageInflicted then
      prompt = "#sk__yuhua2-invoke"
    end
    return player.room:askForSkillInvoke(player, self.name, nil, prompt)
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, self.name)
    if not player.dead and player:getMaxCards() > 0 then
      player.room:addPlayerMark(player, MarkEnum.MinusMaxCards, 1)
    end
    return true
  end,
}
local sk__dengxian = fk.CreateTriggerSkill{
  name = "sk__dengxian",
  frequency = Skill.Wake,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and
      player:usedSkillTimes(self.name, Player.HistoryGame) == 0 and
      player.phase == Player.Start
  end,
  can_wake = function(self, event, target, player, data)
    return player:getMaxCards() == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = room:askForChoice(player, {"phase_draw", "phase_play"}, self.name, "#sk__dengxian-choose")
    room:setPlayerMark(player, self.name, choice)
  end,

  refresh_events = {fk.EventPhaseChanging},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark(self.name) ~= 0 and data.to == Player.Discard
  end,
  on_refresh = function(self, event, target, player, data)
    if player:getMark(self.name) == "phase_draw" then
      data.to = Player.Draw
    else
      data.to = Player.Play
    end
  end,
}
zhugeguo:addSkill(sk__yuhua)
zhugeguo:addSkill(sk__dengxian)
Fk:loadTranslationTable{
  ["sk__zhugeguo"] = "诸葛果",
  ["#sk__zhugeguo"] = "",

  ["sk__yuhua"] = "羽化",
  [":sk__yuhua"] = "当你使用锦囊牌造成伤害时，或当你受到锦囊牌造成的伤害时，你可以摸一张牌并防止此伤害，若如此做，你的手牌上限-1。",
  ["sk__dengxian"] = "登仙",
  [":sk__dengxian"] = "觉醒技，准备阶段，若你的手牌上限为0，你选择一项：1.你的弃牌阶段改为摸牌阶段；2.你的弃牌阶段改为出牌阶段。",
  ["#sk__yuhua1-invoke"] = "羽化：你可以摸一张牌防止你对 %dest 造成的伤害，你手牌上限-1",
  ["#sk__yuhua2-invoke"] = "羽化：你可以摸一张牌防止你受到的伤害，你手牌上限-1",
  ["#sk__dengxian-choose"] = "登仙：选择你本局游戏将弃牌阶段替换为的阶段",

  ["$sk__yuhua1"] = "浩浩乎如冯虚御风。",
  ["$sk__yuhua2"] = "飘飘乎如遗世独立。",
  ["$sk__dengxian1"] = "乘烟得道去，窥得仙人身。",
  ["$sk__dengxian2"] = "踏云飞烟，羽化仙尘。",
  ["~sk__zhugeguo"] = "浮生若梦，终入轮回。",
}

return extension
